//
//  CreateEventViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 8/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CreateEventViewController.h"
#import "CreateEventPeopleViewController.h"
#import "AddressViewController.h"
#import "CreateEventMapViewController.h"
#import "JGoAPI.h"
#import "DateTimeViewController.h"

@implementation NSDictionary (Extension)

- (id)newObjectForKey:(id)aKey
{
    id rez = [self objectForKey:aKey];
    
    return ([rez isKindOfClass:[NSNull class]] ? nil : rez);
}
@end

@implementation CreateEventViewController

@synthesize createCell03,createCell06,createCell07,data;
@synthesize modules,isRefresh,tfTitle,txtDescribe;

- (void)applyAppearance
{
    [self setRightButtonImageActiveName:nil 
                           inactiveName:@"done-button" 
                                 target:self 
                                 action:@selector(done:)];
    
    [self setLeftButtonImageActiveName:nil 
                          inactiveName:@"back-button.png" 
                                target:self 
                                action:@selector(back:)];
    [self setTitle:@"Create Event"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg"]];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(void)dealloc
{
    [createCell03 release];
    [createCell06 release];
    [createCell07 release];
    [data release];
    [modules release];
    [super dealloc];
}
#pragma mark - View lifecycle

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (isRefresh) {
        [self.tableView reloadData];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    isKeyboardShowed = NO;
    NSDictionary *userInfo = [JGoUtility sharedCreateAccount];
    modules = [[userInfo newObjectForKey:@"modules"] retain];
    NSLog(@"%@",modules);
    [self applyAppearance];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;
    //data = [[NSDictionary alloc]init];
    self.data = [[NSMutableDictionary alloc]initWithObjectsAndKeys: @"",@"title",
                                                        @"", @"type",
                                                        @"", @"cirType",
                                                        @"", @"cirValue",
                                                        @"",@"scope", 
                                                        @"",@"submitedDate", 
                                                        @"",@"eventDate",
                                                        @"",@"startTime",                                                                                                                                    
                                                        @"",@"endTime", 
                                                        @"",@"addType", 
                                                        @"",@"address",        
                                                        @"",@"description",
                                                        @"",@"lat",
                                                        @"",@"lon",
                                                        @"",@"selectedLat", 
                                                        @"",@"selectedLon", 
                                                        nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Action

- (void)keyboardDidShow: (NSNotification *) notif{
    isKeyboardShowed = YES;
}

- (void)keyboardDidHide: (NSNotification *) notif{
    isKeyboardShowed = NO;
}


- (void)delayBlock:(MBProgressHUD*)progress
{
    NSString *userId = [[JGoUtility sharedCreateAccount] objectForKey:@"user_id"];
    NSError *error = nil;
    NSString *title = [self.data newObjectForKey:@"title"];
    NSString *type = [self.data newObjectForKey:@"type"];
    NSString *cirType = [self.data newObjectForKey:@"cirType"];
    NSString *cirValue = [self.data newObjectForKey:@"cirValue"];
    NSString *scope = [self.data newObjectForKey:@"scope"];
    NSString *eventDate = [self.data newObjectForKey:@"submitedDate"];
    NSString *startTime = [self.data newObjectForKey:@"startTime"];
    NSString *endTime = [self.data newObjectForKey:@"endTime"];
    NSString *address = [self.data newObjectForKey:@"address"];
    NSString *description = [self.data newObjectForKey:@"description"];
   
    NSString *lat = [JGoUtility sharedLatitude];
    NSString *lon = [JGoUtility sharedLongitude];
    if ([data newObjectForKey:@"addressLat"] != nil) {
        lat = [data newObjectForKey:@"addressLat"];
        lon = [data newObjectForKey:@"addressLon"];
    }
    NSLog(@"DATA = %@",self.data);
    [JGoAPI createNewEventsWithUserId:userId title:title type:type cirType:cirType cirValue:cirValue scope:scope eventDate:eventDate startTime:startTime endTime:endTime address:address description:description lat:lat lon:lon error:&error];
    
    
    if (error) {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    [progress hide:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)done:(id)sender
{
    [tfTitle resignFirstResponder];
    [describe resignFirstResponder];
    if (tfTitle.text == nil) tfTitle.text = @"";
    if (txtDescribe.text == nil) txtDescribe.text = @"";
    
    
    [self.data setObject:txtDescribe.text forKey:@"description"];
    NSLog(@"SUBMITED DATA: %@",self.data);
    //NSLog(@"%@",[self.data objectForKey:@"title"]);
    NSString *date = [self.data newObjectForKey:@"submitedDate"];
    NSString *start = [self.data newObjectForKey:@"startTime"];
    NSString *end = [self.data newObjectForKey:@"endTime"];
    if (date == nil || start == nil || end == nil || date == @"" || start == @"" || end == @"") {
        [JGoUtility showAlert:@"Time of Event is not empty"];
    }else {
        if (isKeyboardShowed) {
            [JGoUtility showProgressInView:[JGoUtility frontFromView:self.view]
                                    target:self
                                     title:@"Creating..."
                                  selector:@selector(delayBlock:)];
            
        }else {
            [JGoUtility showProgressInView:self.view
                                    target:self
                                     title:@"Creating..."
                                  selector:@selector(delayBlock:)];
        }

    }
            
}

#pragma mark - TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 2;
            break;
        case 2:
            return 1;
            break;
        case 3:
            return 3;
            break;
        case 4:
            return 5+[modules count];
            break;
        case 5:
            return 1;
            break;
        case 6:
            return 1;
            break;
        default:
            return 1;
            break;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   switch (indexPath.section) {
       case 5: 
       {
           return 45;         
           break;
       }
       case 6:
       {
           return 182;
           break;
       }
       default:
       {
        return 44;
        break;
       }
   }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 40;
            break;
        case 1:
            return 40;
            break;
        
        case 3:
            return 40;
            break;
        case 4:
            return 40;
            break;
        
        default:
            return 5;
            break;
    }    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    switch (section) {
            
        case 0:
        {
            UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblText.text = @"  Type of Event";
            lblText.backgroundColor = [UIColor clearColor];
            lblText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            return [lblText autorelease];
            break;
        } 
        case 1:
        {
            UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblText.text = @"  Online/Real Life";
            lblText.backgroundColor = [UIColor clearColor];
            lblText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            return [lblText autorelease];
            break;
        }
        case 3:
        {
            UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblText.text = @"  Place";
            lblText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            lblText.backgroundColor = [UIColor clearColor];
            return [lblText autorelease];
            break;
        }
        case 4:
        {
            UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblText.text = @"  Select the circle";
            lblText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            lblText.backgroundColor = [UIColor clearColor];
            return [lblText autorelease];
            break;
        }
        default:
        {
            
            break;
        }
    }
    return nil;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    switch (indexPath.section) {
        case 2:
        {
            static NSString *CreateCell03Identifier = @"CreateCell03";
            cell = [tableView dequeueReusableCellWithIdentifier:CreateCell03Identifier];
            if (cell == nil) {
                cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CreateCell03Identifier] autorelease];
                [[NSBundle mainBundle] loadNibNamed:@"CreateEventCell" owner:self options:nil];
                cell = createCell03;
                self.createCell03 = nil;  
            }

            break;
        } 
        case 5:
        {
            static NSString *CreateCell06Identifier = @"CreateCell06";
            cell = [tableView dequeueReusableCellWithIdentifier:CreateCell06Identifier];
            if (cell == nil) {
                cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CreateCell06Identifier] autorelease];
                [[NSBundle mainBundle] loadNibNamed:@"CreateEventCell" owner:self options:nil];
                cell = createCell06;
                self.createCell06 = nil;  
            }
            
            break;
        }  
        case 6:
        {
            static NSString *CreateCell07Identifier = @"CreateCell07";
            cell = [tableView dequeueReusableCellWithIdentifier:CreateCell07Identifier];
            if (cell == nil) {
                cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CreateCell07Identifier] autorelease];
                [[NSBundle mainBundle] loadNibNamed:@"CreateEventCell" owner:self options:nil];
                cell = createCell07;
                self.createCell07 = nil;  
            }
            
            break;
        } 
        default:
        {
            static NSString *CellIdentifier = @"Cell";
            
            cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            break;
        }
            
    }
    
    cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
    int indexOfFavorite = (5+[modules count]-2);
    int indexOfNearby   = (5+[modules count]-1);
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"Education";
            }
            if (indexPath.row == 1) {
                cell.textLabel.text = @"Social";
            }
            if ([[self.data objectForKey:@"type"] isKindOfClass:[NSNull class]]) break;
            if ([[self.data objectForKey:@"type"] isEqualToString:cell.textLabel.text]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                [cell setSelected:TRUE animated:TRUE];
            }else {
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                [cell setSelected:NO animated:TRUE];

            }
            break;
 
        }
        case 1:
        {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"Online";
            }
            if (indexPath.row == 1) {
                cell.textLabel.text = @"Real Life";
            }
            NSString *currentCellValue = [NSString stringWithFormat:@"%d",indexPath.row];
            if ([[self.data objectForKey:@"scope"] isKindOfClass:[NSNull class]]) break;
            if ([[self.data objectForKey:@"scope"] isEqualToString:currentCellValue]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
                cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                [cell setSelected:TRUE animated:TRUE];
            }else {
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                [cell setSelected:NO animated:TRUE];
                
            }
            
            break;
            
        }
        case 2:
        {
            UILabel *firstText = (UILabel *)[cell viewWithTag:1];
            UILabel *secondText = (UILabel *)[cell viewWithTag:2];
            if ([data newObjectForKey:@"eventDate"] ==@"") {
                break;
            }
            NSString *timeStr = [NSString stringWithFormat:@"%@, %@-%@",[data newObjectForKey:@"eventDate"],[data newObjectForKey:@"startTime"],[data newObjectForKey:@"endTime"]];
            NSLog(@"timeStr= %@",timeStr);
            secondText.text = timeStr;
            firstText.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
            secondText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            break;
            
        }
        case 3:
        {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"Address";
                if ([[self.data newObjectForKey:@"addType"] isEqualToString:@"address"]) {
                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                    [cell setSelected:TRUE animated:TRUE];
                }else {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                    [cell setSelected:NO animated:TRUE];
                    
                }
                
            } else if (indexPath.row == 1){
                cell.textLabel.text = @"Find on map";
                if ([[self.data newObjectForKey:@"addType"] isEqualToString:@"map"]) {
                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                    [cell setSelected:TRUE animated:TRUE];
                }else {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                    [cell setSelected:NO animated:TRUE];
                    
                }
            }else if (indexPath.row == 2){
                cell.textLabel.text = @"Locate me";
                if ([[self.data newObjectForKey:@"addType"] isEqualToString:@"locateme"]) {
                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                    [cell setSelected:TRUE animated:TRUE];
                }else {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                    [cell setSelected:NO animated:TRUE];
                    
                }
            }
            
            break;
            
        }
        case 4:
        {
            
            switch (indexPath.row) {
                case 0:
                {
                    cell.textLabel.text = @"My university";
                    if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                    NSString* type = [NSString stringWithFormat:@"%d",indexPath.row];
                    if ([[self.data objectForKey:@"cirType"] isEqualToString:type]) {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                        [cell setSelected:TRUE animated:TRUE];
                    }else {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                        cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                        [cell setSelected:NO animated:TRUE];
                        
                    }
                    break;
                } 
                case 1:
                {
                    cell.textLabel.text = @"My Class Year";
                    if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                    NSString* type = [NSString stringWithFormat:@"%d",indexPath.row];
                    if ([[self.data objectForKey:@"cirType"] isEqualToString:type]) {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                        [cell setSelected:TRUE animated:TRUE];
                    }else {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                        cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                        [cell setSelected:NO animated:TRUE];
                        
                    }
                    
                    break;
                } 
                case 2:
                {
                    cell.textLabel.text = @"My course";
                    if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                    NSString* type = [NSString stringWithFormat:@"%d",indexPath.row];
                    if ([[self.data objectForKey:@"cirType"] isEqualToString:type]) {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                        cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                        [cell setSelected:TRUE animated:TRUE];
                    }else {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                        cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                        [cell setSelected:NO animated:TRUE];
                        
                    }
                    
                    break;
                } 
                                
                default:
                {
                    if (indexPath.row == indexOfFavorite) {
                        cell.textLabel.text = @"Favorites";
                        if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                        NSString* type = [NSString stringWithFormat:@"%d",4];
                        if ([[self.data objectForKey:@"cirType"] isEqualToString:type]) {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                            [cell setSelected:TRUE animated:TRUE];
                        }else {
                            cell.accessoryType = UITableViewCellAccessoryNone;
                            cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                            [cell setSelected:NO animated:TRUE];
                            
                        }
                        break;
                    } else if (indexPath.row == indexOfNearby) {
                        cell.textLabel.text = @"Nearby";
                        if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                        NSString* type = [NSString stringWithFormat:@"%d",5];
                        if ([[self.data objectForKey:@"cirType"] isEqualToString:type]) {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                            [cell setSelected:TRUE animated:TRUE];
                        }else {
                            cell.accessoryType = UITableViewCellAccessoryNone;
                            cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                            [cell setSelected:NO animated:TRUE];
                            
                        }
                        break;
                    } else {
                        cell.textLabel.text = [modules objectAtIndex:(indexPath.row - 3)];
                        if([[self.data objectForKey:@"cirType"] isKindOfClass:[NSNull class]]) break;
                        NSString* type = [NSString stringWithFormat:@"%d",3];
                        if ([[self.data objectForKey:@"cirType"] isEqualToString:type] &&
                            [[self.data objectForKey:@"cirValue"] isEqualToString:cell.textLabel.text]) {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            cell.textLabel.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
                            [cell setSelected:TRUE animated:TRUE];
                        }else {
                            cell.accessoryType = UITableViewCellAccessoryNone;
                            cell.textLabel.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
                            [cell setSelected:NO animated:TRUE];
                            
                        }
                    }
                    break;
                }
            }
            
            break;
            
        }
        case 5:
        {
            UITextField *txtField = (UITextField *)[cell viewWithTag:1];
            txtField.backgroundColor = [UIColor clearColor];
            cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
            break;
        }
        case 6:
        {
            UITextView *txtView = (UITextView *)[cell viewWithTag:2];
            txtView.backgroundColor = [UIColor clearColor];
            cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
            break;
        }

        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",cell.textLabel.text);
    
    switch (indexPath.section) {
        case 0: 
        {
            NSString *value = cell.textLabel.text;
            [self.data setValue:value forKey:@"type"];
            [self.tableView reloadData];
            break;
        }
        case 1: 
        {
            NSString* value = [NSString stringWithFormat:@"%d",indexPath.row];
            [data setValue:value forKey:@"scope"];
            [self.tableView reloadData];

            break;
        }
        case 2:
        {
          
            DateTimeViewController *dtVC = [[DateTimeViewController alloc]initWithNibName:@"DateTimeViewController" bundle:nil];
            dtVC.data = self.data;
            self.isRefresh = YES;
            [self.navigationController pushViewController:dtVC animated:YES];
            [dtVC release];
            break;
        }
        case 3:
        {
            if (indexPath.row == 0) {
                self.isRefresh = YES;
                [data setObject:@"address" forKey:@"addType"];
                AddressViewController *aVC = [[AddressViewController alloc]initWithNibName:@"AddressViewController" bundle:nil];
                aVC.data = self.data;
                [self.navigationController pushViewController:aVC animated:YES];
                [aVC release];
            } else if (indexPath.row == 1) 
            {
                self.isRefresh = YES;
                [data setObject:@"map" forKey:@"addType"];
                CreateEventMapViewController *mVC = [[CreateEventMapViewController alloc]initWithNibName:@"CreateEventMapViewController" bundle:nil];
                mVC.data = self.data;
                [self.navigationController pushViewController:mVC animated:YES];
                [mVC release];
            }else if (indexPath.row ==2 )
            {
                [data setObject:@"locateme" forKey:@"addType"];
                NSString *lat = [JGoUtility sharedLatitude];
                NSString *lon = [JGoUtility sharedLongitude];
                [data setObject:lat forKey:@"addressLat"];
                [data setObject:lon forKey:@"addressLon"];
                NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,%@&output=json", lat, lon];   
                NSURL *url = [NSURL URLWithString:urlString];
                
                NSString *locationString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
                // NSLog(@"%@",locationString);
                NSDictionary *dic = [locationString JSONValue];
                NSLog(@"%@",dic);
                NSArray *placemarkArr = [dic newObjectForKey:@"Placemark"] ;
                
                NSDictionary *addressDic = [placemarkArr objectAtIndex:0];
                NSString *address = [addressDic newObjectForKey:@"address"];
                NSLog(@"%@",address);
                [data setObject:@"" forKey:@"address"];
                [data setObject:address forKey:@"address"];
                [self.tableView reloadData];
            }
            
            break;
        }
        case 4:
        {
            switch (indexPath.row) {
                case 0:
                {
                    NSString* value = [NSString stringWithFormat:@"%d",indexPath.row];
                    [data setValue:value forKey:@"cirType"];
                    [data setValue:cell.textLabel.text forKey:@"cirValue"];
                    [self.tableView reloadData];
                    break;
                }
                case 1:
                {
                    NSString* value = [NSString stringWithFormat:@"%d",indexPath.row];
                    [data setValue:value forKey:@"cirType"];
                    [data setValue:cell.textLabel.text forKey:@"cirValue"];
                    [self.tableView reloadData];
                    break;
                }
                case 2:
                {
                    NSString* value = [NSString stringWithFormat:@"%d",indexPath.row];
                    [data setValue:value forKey:@"cirType"]; 
                    [data setValue:cell.textLabel.text forKey:@"cirValue"];
                    [self.tableView reloadData];
                    break;
                }
                                    
                default:
                {
                    int indexOfFavorite = (5+[modules count]-2);
                    int indexOfNearby   = (5+[modules count]-1);
                    if (indexPath.row == indexOfFavorite) {
                        NSString* value = [NSString stringWithFormat:@"%d",4];
                        [data setValue:value forKey:@"cirType"];
                        CreateEventPeopleViewController *pVC = [[CreateEventPeopleViewController alloc]initWithNibName:@"CreateEventPeopleViewController" bundle:nil];
                        pVC.isFav = YES;
                        pVC.isNearby = NO;
                        pVC.eventData = self.data;
                        self.isRefresh = YES;
                        
                        [self.navigationController pushViewController:pVC animated:YES];
                        [pVC release];
                       
                    } else if (indexPath.row == indexOfNearby) {
                        NSString* value = [NSString stringWithFormat:@"%d",5];
                        [data setValue:value forKey:@"cirType"];
                        self.isRefresh = YES;
                        CreateEventPeopleViewController *pVC = [[CreateEventPeopleViewController alloc]initWithNibName:@"CreateEventPeopleViewController" bundle:nil];
                        
                        pVC.isNearby = YES;
                        pVC.eventData = self.data;
                        [self.navigationController pushViewController:pVC animated:YES];
                        [pVC release];
                       
                    } else {
                        NSString* value = [NSString stringWithFormat:@"%d",3];
                        [data setValue:value forKey:@"cirType"];
                        [data setValue:cell.textLabel.text forKey:@"cirValue"];  
                        [self.tableView reloadData];
                    }
                    
                    break;
                }
            }
            
            break;
        }
    }
    
}


#pragma mark - TextField Delegete && TextView Delegete

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (id)textField.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 218, 0)];
    [self.tableView scrollToRowAtIndexPath:indexPath 
                          atScrollPosition:UITableViewScrollPositionNone 
                                  animated:YES];
    return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [self.data setObject:textField.text forKey:@"title"];
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                          atScrollPosition:UITableViewScrollPositionNone 
                                  animated:YES];
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UITableViewCell *cell = (id)textView.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 218, 0)];
    [self.tableView scrollToRowAtIndexPath:indexPath 
                          atScrollPosition:UITableViewScrollPositionNone 
                                  animated:YES];
    
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

@end
